﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PipeAuto.DAL.PipeAutoDataSetTableAdapters;

namespace PipeAuto.MgrPlatform
{
    public partial class DeptSelectForm : Form
    {
        public DeptSelectForm()
        {
            InitializeComponent();
        }

        private void DeptSelectForm_Load(object sender, EventArgs e)
        {
            int myLevel = 0;


            while (true)
            {
                try
                {
                    DepartmentsTableAdapter myAdapter = new DepartmentsTableAdapter();

                    var myTable = myAdapter.GetDataByLevel(myLevel);

                    if (myTable.Count==0)
                    {
                        return;
                    }

                    myLevel++;

                    foreach (var myRow in myTable)
                    {
                        if (myRow.Level==0)
                        {
                            deptTreeView.Nodes.Add(new TreeNode { Text = myRow.Title, Tag = myRow.ID, ForeColor = myRow.State == 1 ? Color.Black : Color.Gray });
                            continue;
                        }

                        var myParentNode = MgrPlatformGlobal.LeveledRecurseTreeView(myRow.ParentDeptID, deptTreeView.Nodes[0], myRow.Level - 1);

                        if (myParentNode==null)
                        {
                            MessageBox.Show("数据错误！");
                            return;
                        }

                        myParentNode.Nodes.Add(new TreeNode { Text = myRow.Title, Tag = myRow.ID, ForeColor = myRow.State == 1 ? Color.Black : Color.Gray });
                    }

                    if (selectedLabel.Tag!=null)
                    {
                        var mySelectedNode = MgrPlatformGlobal.LeveledRecurseTreeView(selectedLabel.Tag.ToString(), deptTreeView.Nodes[0]);
                        deptTreeView.SelectedNode = mySelectedNode;

                        while (true)
                        {
                            if (mySelectedNode==null)
                            {
                                break;
                            }

                            mySelectedNode.Expand();
                            mySelectedNode = mySelectedNode.Parent;
                        }
                    }
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return;
                }
            }
        }

        private void deptTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            selectedLabel.Text = deptTreeView.SelectedNode.Text;
            selectedLabel.Tag = deptTreeView.SelectedNode.Tag;
        }

        public Label SelectedLabel
        {
            get
            {
                return this.selectedLabel;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void selectedOkButton_Click(object sender, EventArgs e)
        {
            if (selectedLabel.Tag==null)
            {
                MessageBox.Show("请选择部门！");
                return;
            }

            var myParent = this.Owner as MainForm;

            myParent.UserDeptLinkLabel.Text = deptTreeView.SelectedNode.Text;
            myParent.UserDeptLinkLabel.Tag = deptTreeView.SelectedNode.Tag;

            this.DialogResult = DialogResult.OK;
        }
    }
}
